package com.xmmj.common.refresh

import android.annotation.SuppressLint
import android.content.Context
import android.util.AttributeSet
import android.view.View
import kotlin.jvm.JvmOverloads
import android.widget.RelativeLayout
import com.scwang.smart.refresh.layout.api.RefreshFooter
import android.widget.TextView
import com.scwang.smart.refresh.layout.constant.SpinnerStyle
import com.scwang.smart.refresh.layout.api.RefreshKernel
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.constant.RefreshState
import com.xmmj.common.R

@SuppressLint("RestrictedApi")
class CustomFooter @JvmOverloads constructor(context: Context?, attrs: AttributeSet? = null) :
    RelativeLayout(context, attrs, 0), RefreshFooter {

    private var mNoMoreData = false

    private val tvHint: TextView

    companion object {
        var REFRESH_FOOTER_PULLING = "上拉加载更多"
        var REFRESH_FOOTER_RELEASE = "释放立即加载"
        var REFRESH_FOOTER_LOADING = "正在加载..."
        var REFRESH_FOOTER_REFRESHING = "正在刷新..."
        var REFRESH_FOOTER_FINISH = "加载完成"
        var REFRESH_FOOTER_FAILED = "加载失败"
        var REFRESH_FOOTER_NOTHING = "已经全部加载完毕～"
    }

    init {
        inflate(context, R.layout.layout_custom_footer, this)
        val thisView: View = this
        tvHint = thisView.findViewById(R.id.tv_hint)
    }

    override fun setNoMoreData(noMoreData: Boolean): Boolean {
        if (mNoMoreData != noMoreData) {
            mNoMoreData = noMoreData
            if (noMoreData) {
                tvHint.text = REFRESH_FOOTER_NOTHING
            } else {
                tvHint.text = REFRESH_FOOTER_PULLING
            }
        }
        return true
    }

    override fun getView(): View {
        return this
    }

    override fun getSpinnerStyle(): SpinnerStyle {
        return SpinnerStyle.Translate
    }

    override fun setPrimaryColors(vararg colors: Int) {

    }

    override fun onInitialized(kernel: RefreshKernel, height: Int, maxDragHeight: Int) {

    }

    override fun onMoving(
        isDragging: Boolean,
        percent: Float,
        offset: Int,
        height: Int,
        maxDragHeight: Int
    ) {

    }

    override fun onReleased(refreshLayout: RefreshLayout, height: Int, maxDragHeight: Int) {

    }

    override fun onStartAnimator(refreshLayout: RefreshLayout, height: Int, maxDragHeight: Int) {

    }

    override fun onFinish(refreshLayout: RefreshLayout, success: Boolean): Int {
        if (!mNoMoreData) {
            tvHint.text =
                if (success) REFRESH_FOOTER_FINISH else REFRESH_FOOTER_FAILED
        }
        return 500
    }

    override fun onHorizontalDrag(percentX: Float, offsetX: Int, offsetMax: Int) {

    }

    override fun isSupportHorizontalDrag(): Boolean {
        return false
    }

    override fun onStateChanged(
        refreshLayout: RefreshLayout,
        oldState: RefreshState,
        newState: RefreshState
    ) {
        if (!mNoMoreData) {
            when (newState) {
                RefreshState.None, RefreshState.PullUpToLoad -> tvHint.text = REFRESH_FOOTER_PULLING
                RefreshState.Loading, RefreshState.LoadReleased -> tvHint.text =
                    REFRESH_FOOTER_LOADING
                RefreshState.ReleaseToLoad -> tvHint.text = REFRESH_FOOTER_RELEASE
                RefreshState.Refreshing -> tvHint.text = REFRESH_FOOTER_REFRESHING
            }
        }
    }
}